package com.eqdd.yiqidian.ui.gongneng.renshi.yuangongdangan.ruzhuyaoqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.InvitePersonListActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.Person;
import com.eqdd.yiqidian.bean.SectionArcBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.ui.select.SelectFromPhoneActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.utils.ValidatorUtil;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.Observable;

public class InvitePersonListActivity extends BaseActivity {
    private InvitePersonListActivityCustom dataBinding;
    private BaseQuickAdapter<Person,BaseViewHolder> quickAdapter;
    private ArrayList<Person> quickAllContacts = new ArrayList<>();
    //    private RenwuStaffBean staffBean;
    private SectionArcBean sectionArcBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite_person_list);
        initTopTitleBar(View.VISIBLE, "受邀人员列表");
        initTopRightText("邀请", v -> {
            if (quickAdapter.getData().size() > 0) {
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < quickAdapter.getData().size(); i++) {
                    strings.add(quickAdapter.getData().get(i).getPhone());
                }
                new HttpPresneter.Builder<String>(HttpConfig.HR_INVITE, String.class, this)
                        .Params("comid", user.getComid())
                        .Params("uid", user.getUname())
                        .Params("name", user.getName())
                        .Params("entel", strings.toString())
                        .Params("ensec", sectionArcBean.getCareepostname())
                        .Params("enpost", sectionArcBean.getCareename())
                        .Params("enpostnum", sectionArcBean.getId()+"")
                        .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                            @Override
                            public void onError(Throwable e) {

                                System.out.println(e.toString());
                            }

                            @Override
                            public void onNext(Object o) {
                                BaseBean baseBean = GsonUtils.changeGsonToBean((String) o, BaseBean.class);
                                ToastUtil.showShort(baseBean.getMsg());
                                if (baseBean.getStatus() == 200) {
                                    finish();
                                }
                            }
                        })
                        .build()
                        .post();
            } else {
                ToastUtil.showShort("你还没选择人呢");
            }
        });
    }

    @Override
    public void initData() {
        sectionArcBean = (SectionArcBean) getIntent().getSerializableExtra(Config.CAREER_INFO);
    }

    @Override
    public void setView() {
        if (quickAdapter == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            quickAdapter = new BaseQuickAdapter<Person, BaseViewHolder>(R.layout.list_item_08,new ArrayList()) {
                @Override
                protected void convert(BaseViewHolder helper, Person item) {

                    helper.setText(R.id.tv_upper_content, item.getName())
                            .setText(R.id.tv_down_content, item.getPhone());
                }
            };
            dataBinding.recyclerView.setAdapter(quickAdapter);
        }

        RxView.clicks(dataBinding.btAdd)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe((o) -> {
                    if (!TextUtils.isEmpty(dataBinding.etInviteNum.getText()) && ValidatorUtil.isMobile(dataBinding.etInviteNum.getText().toString())) {
                        for (Person person : quickAdapter.getData()) {
                            if (person.getPhone().equals(dataBinding.etInviteNum.getText().toString().trim())) {
                                ToastUtil.showShort("受邀人已在邀请队列中");
                                return;
                            }
                        }
                        Person person = new Person();
                        person.setPhone(dataBinding.etInviteNum.getText().toString().trim());
                        person.setName(dataBinding.etInviteNum.getText().toString().trim());
                        quickAdapter.addData(person);
                    } else {
                        ToastUtil.showShort("请输入正确的手机号");
                    }
                });
    }



    @Override
    public void onClick(View v) {

        super.onClick(v);
        switch (v.getId()) {
            case R.id.floatBt:
                Bundle bundle = new Bundle();
                bundle.putInt(Config.MAX_NUM, 5);
                bundle.putParcelableArrayList(Config.CONTRACTS, (ArrayList<Person>) quickAdapter.getData());
                startActivityForResult(SelectFromPhoneActivity.class, bundle, Config.SELECT_CONTRACT);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.SELECT_CONTRACT) {
                ArrayList<Person> personList = data.getParcelableArrayListExtra(Config.CONTRACTS);
                Observable.from(personList)
                        .filter(person -> {
                            for (int i = 0; i < quickAdapter.getData().size(); i++) {
                                if (quickAdapter.getItem(i).getPhone().equals(person.getPhone())){
                                    return false;
                                }
                            }
                            return true;
                        })
                        .subscribe(p->{
                            quickAdapter.addData(p);
                        });
            }
        }
    }
}
